package abc.com.abc.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.ResultReceiver;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import abc.com.abc.R;

public class ChangeUserInfoActivity extends AppCompatActivity {
    private TextView tv_main_title,tv_save,tv_back;
    private RelativeLayout rl_title_bar;
    private String title,content;
    private int flag;//flag为1时候修改姓名，为2时修改教练名，为3时，修改电话号码。
    private EditText et_content;
    private ImageView iv_delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_user_info);
        //设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
    }

    private void init() {
        //从个人资料页面传过来的标题和内容
        Bundle bundle = getIntent().getExtras();

        title = bundle.getString("title");
        content = bundle.getString("content");
        Log.i("ddl","title+content="+title+"+"+content);
        flag = bundle.getInt("flag",0);

        tv_main_title = findViewById(R.id.tv_main_title);
        tv_main_title.setText(title);
        rl_title_bar = findViewById(R.id.title_Bar);
        rl_title_bar.setBackgroundColor(Color.parseColor("#30b4ff"));
        tv_back = findViewById(R.id.tv_back);
        tv_save = findViewById(R.id.tv_save);
        tv_save.setVisibility(View.VISIBLE);//设置保存按钮为可见
        et_content = findViewById(R.id.et_content);
        iv_delete = findViewById(R.id.delete);
        if (!TextUtils.isEmpty(content)){
            et_content.setText(content);
            et_content.setSelection(content.length());//全选里面的内容

        }
        contentListener();//监听个人资料修改界面输入的文字
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChangeUserInfoActivity.this.finish();
            }
        });
        iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_content.setText("");
            }
        });
        //保存
        tv_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("test", "onClick: save111");
                Intent data = new Intent();
                String  etContent = et_content.getText().toString().trim();//获取输入的字符
                switch (flag){
                    case 1:
                        if (!TextUtils.isEmpty(etContent)){
                            data.putExtra("name",etContent);
                            setResult(RESULT_OK,data);
                            Toast.makeText(ChangeUserInfoActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                            Log.i("test","保存成功"+etContent);
                        }else {
                            Toast.makeText(ChangeUserInfoActivity.this,"姓名不能为空",Toast.LENGTH_SHORT).show();
                            Log.i("test","姓名不能为空");
                        }
                        break;
                    case 2:
                        if (!TextUtils.isEmpty(etContent)){
                            data.putExtra("jiaolian",etContent);
                            setResult(RESULT_OK,data);
                            Log.i("test","保存成功"+etContent);
                            Toast.makeText(ChangeUserInfoActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(ChangeUserInfoActivity.this,"教练名不能为空",Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case 3:
                        if (!TextUtils.isEmpty(etContent)){
                            data.putExtra("phone",etContent);
                            setResult(RESULT_OK,data);
                            Log.i("test","保存成功"+etContent);
                            Toast.makeText(ChangeUserInfoActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(ChangeUserInfoActivity.this,"电话不能为空",Toast.LENGTH_SHORT).show();
                        }
                        break;

                }
            }
        });
    }
    //监听个人资料修改界面输入的文字
    private void contentListener() {
        //检测EditText中输入的字数长度
        //after这个方法就是在EditText内容已经改变之后调用
        //TextChanged是在Text改变过程中触发调用的，
        //befor之前
        et_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
              String editable =  et_content.getText().toString();
                int len = editable.length();//输入文本的长度
                if (len>0){
                    iv_delete.setVisibility(View.VISIBLE);
                }else {
                    iv_delete.setVisibility(View.GONE);
                }
                switch (flag){
                    case 1://name
                        //len最多8
                        if (len > 8){
                            int selEndIndex = Selection.getSelectionEnd(editable);
                            String str = et_content.getText().toString();
                            //劫取新字符
                            String newStr = str.substring(0,8);
                            et_content.setText(newStr);
                            editable = et_content.getText().toString();
                            //新字符的长度
                            int newLen = editable.length();
                            //旧的超过新的长度
                            if (selEndIndex > newLen){
                                selEndIndex = editable.length();
                            }
                            //设置新广所在的位置
                            Selection.setSelection(et_content.getText(),selEndIndex);
                        }
                        break;
                    case 2://jiaolian
                        if (len>10){
                            int selEndIndex = Selection.getSelectionEnd(et_content.getText());
                            String str = et_content.getText().toString();
                            //劫取新字符
                            String newStr = str.substring(0,10);
                            et_content.setText(newStr);

                            int newLen = et_content.getText().length();
                            if (selEndIndex > newLen){
                                selEndIndex = et_content.getText().length();
                            }
                            Selection.setSelection(et_content.getText(),selEndIndex);
                        }
                        break;
                    case 3:
                        if (len > 11){
                            int selEndIndex = Selection.getSelectionEnd(et_content.getText());
                            String str = et_content.getText().toString();
                            //劫取新字符
                            String newStr = str.substring(0,11);
                            et_content.setText(newStr);
                            int newLen = et_content.getText().length();
                            if (selEndIndex > newLen){
                                selEndIndex = et_content.getText().length();
                            }
                            Selection.setSelection(et_content.getText(),selEndIndex);
                        }
                        break;
                        default:
                            break;
                        }


                }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}
